package net.huashitong.ssydt.provider.task.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.huashitong.ssydt.provider.annotations.LoginRequired;
import net.huashitong.ssydt.provider.task.service.TaskService;
import net.huashitong.ssydt.provider.web.model.Result;
import net.huashitong.ssydt.provider.web.utils.ResultUtils;
import net.huashitong.ssydt.security.utils.UserUtils;
import net.huashitong.ssydt.task.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2018.01.10
 */
@Api(value = "/api/task/", tags = {"任务模块"})
@RestController
@RequestMapping("/api/task/")
public class ApiTaskController {

    private TaskService taskService;

    @Autowired
    public ApiTaskController(TaskService taskService) {
        this.taskService = taskService;
    }

    @ApiOperation(value = "卡包集列表", notes = "卡包集列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @GetMapping("/card/bags")
    public Result<ArrayList<YearCardBagVO>> getCardBags(@Valid CardBagInVO inVO) {
        String userId = UserUtils.getUserId();
        List<YearCardBagVO> list = taskService.getCardBags(inVO, userId);
        return ResultUtils.getSuccessResultList(list);
    }

    @ApiOperation(value = "我的卡包信息", notes = "我的卡包信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @GetMapping("/card/info")
    public Result<CardInfoVO> getCardInfo() {
        String userId = UserUtils.getUserId();
        CardInfoVO vo = taskService.getCardInfo(userId);
        return ResultUtils.getSuccessResults(vo);
    }

    @ApiOperation(value = "卡包列表", notes = "卡包列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @GetMapping("/cards")
    public Result<ArrayList<CardsOutVO>> getCards(@ApiParam(value = "年月", required = true)
                                                  @RequestParam("yearMonths") String yearMonths) {
        String userId = UserUtils.getUserId();
        List<CardsOutVO> voList = taskService.getCards(userId, yearMonths);
        return ResultUtils.getSuccessResultList(voList);
    }


    @ApiOperation(value = "获取未读卡包数量", notes = "获取未读卡包数量", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @GetMapping("/cards/number")
    public Result<Integer> getCardsNumber() {
        String userId = UserUtils.getUserId();
        Integer number = taskService.getCardsNumber(userId);
        return ResultUtils.getSuccessResults(number);
    }

    @ApiOperation(value = "申请获得卡片", notes = "申请获得卡片", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @PostMapping("/cards/question/{questionId}")
    public Result<Boolean> postCardsQuestionId(@ApiParam(value = "题目id", required = true)
                                               @PathVariable("questionId") Long questionId) {
        String userId = UserUtils.getUserId();
        boolean success = taskService.postCardsQuestionId(userId, questionId);
        return ResultUtils.getSuccessResults(success);
    }

    @ApiOperation(value = "设置卡包为已读", notes = "设置卡包为已读", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @PutMapping("/cards/{cardId}")
    public Result<String> putCardsByCardId(@ApiParam(value = "卡片id", required = true)
                                           @PathVariable("cardId") Long cardId) {
        String userId = UserUtils.getUserId();
        taskService.putCardsByCardId(userId, cardId);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "设置全部卡包为已读", notes = "设置全部卡包为已读", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @PutMapping("/cards")
    public Result<String> putCards() {
        String userId = UserUtils.getUserId();
        taskService.putCards(userId);
        return ResultUtils.getSuccessResults();
    }


    @ApiOperation(value = "勋章列表", notes = "勋章列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @GetMapping("/medals")
    public Result<ArrayList<TaskMedalVO>> getMedals() {
        String userId = UserUtils.getUserId();
        List<TaskMedalVO> list = taskService.getMedals(userId);
        return ResultUtils.getSuccessResultList(list);
    }


    @ApiOperation(value = "通过用户id获取勋章列表", notes = "通过用户id获取勋章列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/medals/userId/{userId}")
    public Result<ArrayList<TaskMedalVO>> getMedalsByUserId(@PathVariable("userId") String userId) {
        List<TaskMedalVO> list = taskService.getMedals(userId);
        return ResultUtils.getSuccessResultList(list);
    }

    @ApiOperation(value = "任务列表", notes = "任务列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @GetMapping("/tasks")
    public Result<ArrayList<TasksVO>> getTasks() {
        String userId = UserUtils.getUserId();
        List<TasksVO> list = taskService.getTasks(userId);
        return ResultUtils.getSuccessResultList(list);
    }

    @ApiOperation(value = "领取任务奖励", notes = "领取任务奖励", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @PostMapping("/tasks/{taskRecordId}")
    public Result<String> postTasks(@ApiParam(value = "任务记录id") @PathVariable("taskRecordId") Long taskRecordId) {
        String userId = UserUtils.getUserId();
        taskService.postTasks(userId, taskRecordId);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "查询是否签到", notes = "查询是否签到", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @GetMapping("/sign")
    public Result<Boolean> getSign() {
        String userId = UserUtils.getUserId();
        Boolean isSign = taskService.getSign(userId);
        return ResultUtils.getSuccessResults(isSign);
    }


    @ApiOperation(value = "执行签到", notes = "执行签到", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @PostMapping("/sign")
    public Result<Long> postSign() {
        String userId = UserUtils.getUserId();
        long goldNumber = taskService.postSign(userId);
        return ResultUtils.getSuccessResults(goldNumber);
    }


}
